package com.yufei.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yufei.dao.GoodsDao;
import com.yufei.domain.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
// 允许跨域
@CrossOrigin
public class GoodsController {
    @Autowired
    private GoodsDao goodsDao;

    // 查询所有商品
    @GetMapping("/goods")
    public List<Goods> getAll() {
        return goodsDao.selectList(null);
    }

    // 分页查询
    @GetMapping("/goods/{current}")
    public IPage<Goods> selectPage(@PathVariable Integer current) {
        IPage page = new Page(current, 12);
        goodsDao.selectPage(page, null);
        return page;
    }

    // 删除
    @PostMapping("/deleteGoods/{id}")
    public String delete(@PathVariable Integer id) {
        goodsDao.deleteById(id);
        return "OK";
    }

    // 添加库存
    @PostMapping("/modifyinventory")
    public String modifyinventory(@RequestBody Goods goods) {
        goodsDao.updateById(goods);
        return "OK";
    }

    // 添加商品
    @PostMapping("/addGoods")
    public String addGoods(@RequestBody Goods goods) {
        goodsDao.insert(goods);
        return "OK";
    }

    // 条件查询
    @GetMapping("/selectNameGoods/{name}")
    public List selectName(@PathVariable String name) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.like("name", name);
        return goodsDao.selectList(qw);
    }

    // id查询
    @GetMapping("/select/{id}")
    public  List selectId(@PathVariable Number id) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.like("id", id);
        return goodsDao.selectList(qw);
    }
}
